// pages/video/video.js
import request from "../../utils/request";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        videoGroupList:[],
        navId:'',
        videoList:[],
        videoId:'',
        videoUpdateTime:[],
        isTriggered:false,
        offset:1
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getVideoGroupListData()
    },

    async getVideoGroupListData(){
        let videoGroupListData = await request('/video/group/list')
        this.setData({
            videoGroupList:videoGroupListData.data.slice(0,14),
            navId:videoGroupListData.data[0].id
        })
        this.getVideoList(this.data.navId)
    },

    async getVideoList(navId){
        let videoListData = await request('/video/group',{id:navId});
        wx.hideLoading();
        let videoInfoList=[]
        videoListData.datas.forEach(i =>{
            videoInfoList.push({
                id:i.data.vid,
                title:i.data.title,
                creator:i.data.creator,
                commentCount:i.data.commentCount,
                praisedCount:i.data.praisedCount,
                avatarUrl:i.data.creator.avatarUrl,
                nickname:i.data.creator.nickname,
                coverUrl:i.data.coverUrl,
                videoUrl:""
            })
        })
        videoInfoList.forEach(i =>{
            let result =request('/video/url',{id:i.id}).then(r =>{
                i.videoUrl = r.urls[0].url
                this.setData({
                    videoList: videoInfoList
                })
            })
        })
        this.setData({
            isTriggered:false
        })


    },


    changeNav(event){
      let navId = event.currentTarget.id
      this.setData({
          navId:navId*1,
          videoList:[],
          offset:1
      });

      wx.showLoading({
          title:"加载中"
      })

      this.getVideoList(this.data.navId)
    },

    handlePlay(event){
        let vid=event.currentTarget.id
        this.vid!==vid && this.videoContext && this.videoContext.stop()
        this.vid = vid
        this.setData({
            videoId:vid
        })
        this.videoContext = wx.createVideoContext(vid)
        let {videoUpdateTime} = this.data
        let videoItem = videoUpdateTime.find(item=>item.vid===vid)
        if(videoItem){
            this.videoContext.seek(videoItem.currentTime)
        }
    },

    handleTimeUpdate(event){
      let videoObj = {vid:event.currentTarget.id,currentTime:event.detail.currentTime}
      let {videoUpdateTime} = this.data
      let videoItem=videoUpdateTime.find(item=>item.vid===videoObj.vid)
      if(videoItem){
          videoItem.currentTime=videoObj.currentTime
      }else {
          videoUpdateTime.push(videoObj)
      }
      this.setData({
          videoUpdateTime
      })
    },

    handleEnded(event){
        let {videoUpdateTime} = this.data
        videoUpdateTime.splice(videoUpdateTime.findIndex(item=>item.vid===event.currentTarget.id),1)
        this.setData({
            videoUpdateTime
        })
    },

    handleRefresh(){
        this.getVideoList(this.data.navId)
    },

    async handleToLower(){
        let index=this.data.offset
        let videoListData = await request('/video/group',{id:this.data.navId,offset:index});
        wx.hideLoading();
        let videoInfoList=[]
        let {videoList} = this.data
        videoListData.datas.forEach(i =>{
            videoInfoList.push({
                id:i.data.vid,
                title:i.data.title,
                creator:i.data.creator,
                commentCount:i.data.commentCount,
                praisedCount:i.data.praisedCount,
                avatarUrl:i.data.creator.avatarUrl,
                nickname:i.data.creator.nickname,
                coverUrl:i.data.coverUrl,
                videoUrl:""
            })
        })
        videoInfoList.forEach(i =>{
            let result =request('/video/url',{id:i.id}).then(r =>{
                i.videoUrl = r.urls[0].url
                this.setData({
                    videoList: [...videoList,...videoInfoList]
                })
            })
        })
        this.setData({
            offset:++index
        })
    },

    toSearch(){
      wx.navigateTo({
          url:'/pages/search/search'
      })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})